<?php
/**
 * 地区组织模块控制器
 * 
 */
class regionAction extends apiAction {
	
	/**
	 * 获得大区列表
	 */
	public function areas(){
		$data = array();
		//限制查找大区的条件
		$map = array('status'=>1,'pid'=>0);
		if(isset($this->user['areaId'])){
			$map['id'] = array('in', $this->user['areaId']);
		}
		$regions = D('region')->where($map)->select();
		if($regions){
			foreach ($regions as $row){
				$data[] = array('areaId'=>$row['id'], 'areaName'=>$row['name']);
			}
		}
		$this->ajaxReturn(200, L('success'), $data);
	}

	/**
	 * 根据大区 id获得管辖的城市列表
	 */
	public function cities(){
		//大区信息
		$areaId = I('areaId',NULL);
		$area	= D('region')->where(array('id'=>$areaId))->find();
		$data['areaId'] = $area['id'];
		$data['areaName'] = $area['name'];
		//限制查找城市的条件
		$map = array('status'=>1,'pid'=>$areaId);
		if(isset($this->user['cityId'])){
			$map['id'] = array('in', $this->user['cityId']);
		}
		//查找城市
		$regions= D('region')->where($map)->select();
		
		if($regions){
			foreach ($regions as $row){
				$data['cities'][] = array('cityId'=>$row['id'], 'cityName'=>$row['name']);
			}
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 根据城市 id获得管辖的“微区域”列表
	 */
	public function microAreas(){
		//城市信息
		$cityId = I('cityId',NULL);
		$city	= D('region')->where(array('id'=>$cityId))->find();
		$data['cityId'] = $city['id'];
		$data['cityName'] = $city['name'];
		
		//限制查找片区条件
		$map = array('status'=>1,'pid'=>$cityId);
		if(isset($this->user['microAreaId'])){
			$map['id'] = array('in', $this->user['microAreaId']);
		}
		//片区信息
		$regions= D('region')->where($map)->order("`name`")->select();
		if($regions){
			foreach ($regions as $row){
				$data['microAreas'][] = array('microAreaId'=>$row['id'], 'microAreaName'=>$row['name']);
			}
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 根据微区域id返回营业厅列表
	 */
	public function stores(){
		//获得微区域id
		$microAreaId = I('microAreaId', NULL);
		//获得微区域信息
		$microArea	= D('region')->where(array('id'=>$microAreaId))->find();
		
		$data['microAreaId'] = $microArea['id'];
		$data['microAreaName'] = $microArea['name'];
		
		//限制查找营业厅条件
		$map = array('status'=>1,'regionId'=>$microAreaId);
		
		if(isset($this->user['storeId']) && $this->user['storeId']){
			$map['id'] = $this->user['storeId'];
		}
		
		//获得指定微区域下的营业厅
		$stores = D('store')->where($map)->order("`name`")->select();
		
		if($stores){
			foreach ($stores as $row){
				$data['stores'][] = array('storeId'=>$row['id'], 'storeName'=>$row['name'], 'gps'=>$row['gps']);
			}
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
}

/* End of file regionAction.class.php */